var abs = new verjs_select();
var clicked=0;
$(function () {
    if($("#customerType").val()==0 ){
        //企业客户 企业客户按三个必填（电子多一项邮箱）

        $(".individual-customer").show()

    }else{
        //1 个人客户 个人客户时三个不填
        $(".individual-customer").hide()

    }


    // 初始化下拉菜单
    $('.filter-content select').selectpicker({
        noneSelectedText: '请选择',
        sanitize: true,
        size: 10
    });
    // 初始化日期选择控件
    $('.date-input').datepicker({
        language: "zh-CN",
        autoclose: true,
        clearBtn: true,
        todayHighlight: true,
        format: "yyyy-mm-dd"
    }).on('hide',function(e) {
        if($('.apply-form').data('bootstrapValidator')!=undefined){
            $('.apply-form').data('bootstrapValidator')
                .updateStatus('invoiceTm', 'NOT_VALIDATED',null)
                .validateField('invoiceTm');
        }
    })

    loadCompanySelect();

    $(".addressPhone1").show()
    $(".addressPhones").hide()
    $(".bankAddressAccount1").show()
    $(".bankAddressAccounts").hide()

    $('#invoiceType').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        clicked=clickedIndex
        if(clickedIndex==3){
            // if($("#customerType").val()==0){
            //     $(".email-company").show()
            // }
            $(".email-company").hide()


            $(".paper-special label").addClass("label-required")

            $(".paper-special #addressPhone").attr("name","addressPhone")
            $(".paper-special #bankAddressAccount").attr("name","bankAddressAccount")
            $(".paper-special #phone").attr("name","phone")
            $(".paper-special #bankAccount").attr("name","bankAccount")
            if($(".apply-form").data('bootstrapValidator')!=undefined){
                // $(".apply-form").data('bootstrapValidator').destroy();
                // $('.apply-form').data('bootstrapValidator', null);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('addressPhone', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAddressAccount', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('phone', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAccount', true);
            }





            // $(".addressPhone1").show()
            // $(".addressPhones").hide()
            // $(".bankAddressAccount1").show()
            // $(".bankAddressAccounts").hide()
        }else{
            //$("#customerType").val()==0 && clicked==2
            if(clicked==2){

                $(".email-company").show()
            }else{
                $(".email-company").hide()
            }

            $(".paper-special label").removeClass("label-required")

            $(".paper-special input").removeAttr("name")
            if($(".apply-form").data('bootstrapValidator')!=undefined){
                // $(".apply-form").data('bootstrapValidator').destroy();
                // $('.apply-form').data('bootstrapValidator', null);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('addressPhone', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAddressAccount', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('phone', true);
                $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAccount', true);
            }
            // $('.apply-form').data('bootstrapValidator').enableFieldValidators('addressPhone', true);
            // $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAddressAccount', true);
            // $('.apply-form').data('bootstrapValidator').enableFieldValidators('phone', true);
            // $('.apply-form').data('bootstrapValidator').enableFieldValidators('bankAccount', true);


            // $(".apply-form").bootstrapValidator('removeField','addressPhone');

            //
            // $(".addressPhone1").hide()
            // $(".addressPhones").show()
            // $(".bankAddressAccount1").hide()
            // $(".bankAddressAccounts").show()

        }


    })



});

function addApply() {

    var residueMoney = $("#residueMoney").text();
    var invoiceMoney = $("#invoiceMoney").val();
    var remark = $("#remark").val();
    // if (!invoiceMoney){
    //     promptInfo("请填写开票金额！");
    //     return;
    // }
    if(invoiceMoney==0){
        promptInfo("输入金额不能为零！");
        return;
    }
    if (Number(residueMoney) < Number(invoiceMoney)){
        promptInfo("申请开票金额不能大于订单未开发票金额！");
        return;
    }
    var productOrderId = $("#orderId").val();
    var companyId = $("#searchCompanySelect").val();
    // if (!companyId){
    //     promptInfo("请选择开票公司！");
    //     return;
    // }
    var invoiceTm = $("#invoiceTm").val();
    // if (!invoiceTm){
    //     promptInfo("请选择开票日期！");
    //     return;
    // }
    var invoiceTitle = $("#invoiceTitle").val();
    // if (!invoiceTitle){
    //     promptInfo("请填写发票抬头！");
    //     return;
    // }

    $('.apply-form').bootstrapValidator({
        excluded: [":hidden",":disabled"],
        fields: {
            invoiceType: {
                validators: {
                    notEmpty: {
                        message: '开票类型不能为空'
                    }
                }
            },

            searchCompanySelect: {
                validators: {
                    notEmpty: {
                        message: '开票公司不能为空'
                    }
                }
            },
            invoiceTm: {
                validators: {
                    notEmpty: {
                        message: '开票日期不能为空'
                    }
                }
            },
            invoiceMoney: {
                validators: {
                    notEmpty: {
                        message: '开票金额不能为空'
                    }
                }
            },
            sendEmail: {
                validators: {
                    notEmpty: {
                        message: '邮箱不能为空'
                    },
                    emailAddress: {
                        message: '邮箱格式不正确'
                    }
                }
            },
            invoiceTitle: {
                validators: {
                    notEmpty: {
                        message: '纳税人识别号不能为空'
                    }
                }
            },
            taxNumber: {
                validators: {
                    notEmpty: {
                        message: '发票抬头不能为空'
                    }
                }
            },
            addressPhone: {
                validators: {
                    notEmpty: {
                        message: '地址不能为空'
                    }
                }
            },
            bankAddressAccount: {
                validators: {
                    notEmpty: {
                        message: '开户行不能为空'
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: '电话不能为空'
                    },
                    stringLength:{
                        max:12,
                        message:'请输入正确的联系方式'
                    }
                }
            },
            bankAccount: {
                validators: {
                    notEmpty: {
                        message: '账号不能为空'
                    }
                }
            },

        }
    })




    var bootstrapValidator = $(".apply-form").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (!bootstrapValidator.isValid()) {
        return;
    }




    var jsonData ={
        "productOrderId":productOrderId,
        "companyId":companyId,
        "invoiceTm":invoiceTm,
        "invoiceMoney":invoiceMoney,
        "invoiceTitle":invoiceTitle,
        "taxNumber":$("#taxNumber").val(),
        "address":$(".addressPhone").val(),
        "bankAddress":$(".bankAddressAccount").val(),
        "phone":$("#phone").val(),
        "bankAccount":$("#bankAccount").val(),
        "sendEmail":$("#sendEmail").val(),
        "invoiceType":$("#invoiceType").val(),
        "remark":$("#remark").val(),
    }
    $.ajax({
        url: '/orderInvoice/addOrderInvoice',
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(jsonData),
        success: (function (data) {
            if (data.code == "000000") {
                $.alert({
                    title: '提示',
                    content: '操作成功！',
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'btn-primary',
                            action: function () {
                                window.location.reload();
                                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                                var navTab = parent.$multitabs._getNavTab($thisTabs);
                                closeTab(navTab);
                                openTab("订单列表","/front/orderManager/userOrderForm");
                                //delStorageAndRemove(navTab);
                            }
                        }
                    }
                });
            } else {
                $.alert(data.message)
            }
        }),
        error: (function () {
            $.alert("保存异常！");
        })
    });
}

/**
 * 消息提示弹出框
 * @param content  ---提示内容
 */
function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}

function resetApply() {
    // window.location.reload();
    $("#invoiceType").val('')
    $("#searchCompanySelect").val('')
    $("#invoiceTm").val('')
    $("#invoiceMoney").val('')
    $("#sendEmail").val('')
    $("#addressPhone").val('')
    $("#phone").val('')
    $("#bankAddressAccount").val('')
    $("#bankAccount").val('')
    $('select').selectpicker('refresh');
}

/**
 * 加载公司下拉列表
 */
function loadCompanySelect() {
    // $.getJSON("/companyInvoice/getCompanyListByCompanyId", function (data) {
    //     if (data.code == '000000') {
    //         var companyOptionHtml = '<option value="">请选择开票公司</option>';
    //         data.data.map(function (item) {
    //             companyOptionHtml += '<option value="' + item.useCompanyId + '">' + item.useCompanyName + '</option>';
    //         });
    //         $("#searchCompanySelect").html("");
    //         $('#searchCompanySelect').append(companyOptionHtml);
    //         $('#searchCompanySelect').selectpicker('refresh');
    //     }
    // });
    $.ajax({
        url:"/sysInvoiceCompany/selectCompanyInvoiceCompany?companyId="+$("#companyId").val(),
        method:"post",
        success:function (data) {
            if (data.code == '000000') {
                var companyOptionHtml = '<option value="">请选择开票公司</option>';
                data.data.map(function (item) {
                    companyOptionHtml += '<option value="' + item.invoiceCompanyId + '">' + item.companyName + '</option>';
                });
                $("#searchCompanySelect").html("");
                $('#searchCompanySelect').append(companyOptionHtml);
                $('#searchCompanySelect').selectpicker('refresh');
            }else{
                $.alert(data.message)
            }
        },
        error:function (err) {
            $.alert(err.message)
        }
    })
}